package com.vcredit.creditcat.service.msg;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.msg.SystemNoticeDto;
import com.vcredit.creditcat.dto.msg.SystemNoticeListDto;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.framework.annotation.Log;

@Service
public class SystemMessageService {
	
	private static final String STR1 = "****";
	private static final String STR2 = "成功贷款";
	private static final String STR3 = "元";
	private static final String STR4 = "<font color=\"#FF0000\">";
	private static final String STR5 = "</font>";
	private static final String STR6 = "恭喜手机号为";
	private static final String STR7 = "的";
	private static final String STR8 = "先生";
	private static final String STR9 = "女士";
	private static final String STR10 = "男";
	private static final String STR12 = "X";
	
	@Autowired
	private CustProductApplyService custProductApplyService;

	@Log
	public SystemNoticeListDto loanSuccessMsgList() {
		SystemNoticeListDto result = new SystemNoticeListDto();
		List<SystemNoticeDto> msgList = result.getNotices();
		List<Map<String,Object>> list = custProductApplyService.applySuccessList();
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < list.size(); i++) {
			Map<String,Object> map = list.get(i);
			String userName = (String)map.get("userName");
			Integer amt = (Integer)map.get("creditAmt");
			String idName = map.get("idName") == null ? null : (String)map.get("idName");
			String idSex = map.get("idSex") == null ? null : (String)map.get("idSex");
			
			// 金额在1000-20000之间
			if(amt == 0 || amt < 100000 || amt > 2000000) {
				amt = (Integer) ((random.nextInt(20) + 1) * 1000);
			} else {
				amt = amt / 100;
			}
			
			if(StringUtils.isBlank(idName)) {
				idName = STR12;
			} else {
				idName = idName.substring(0,1);
			}
			
			if(StringUtils.isBlank(idSex)) {
				if(i%2 == 0) {
					idSex = STR8;
				} else {
					idSex = STR9;
				}
			} else {
				if(STR10.equals(idSex)) {
					idSex = STR8;
				} else {
					idSex = STR9;
				}
			}
			
			sb.append(STR6);// 恭喜手机号为
			sb.append(userName.substring(0,3)); // 手机号前3位
			sb.append(STR1);
			sb.append(userName.substring(userName.length() - 4)); // 手机号后4位
			sb.append(STR7);// 的
			sb.append(idName); // 姓
			sb.append(idSex); // 先生/女士
			sb.append(STR2);
			sb.append(STR4);
			sb.append(amt);
			sb.append(STR5);
			sb.append(STR3);
			msgList.add(new SystemNoticeDto(sb.toString()));
			sb.delete(0, sb.length());
		}
		return result;
	}
}
